//
//  LBBoundingIponeVC.m
//  V-Learn
//
//  Created by Apple on 2018/12/27.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "LBBoundingIponeVC.h"
#import "NSString+Category.h"
#import "YYModel.h"
#import "UserModel.h"
#import "BRAPPViewModel.h"
#import "LBTabBarViewController.h"
#import "UIButton+Category.h"
#import "UIColor+BRCategory.h"
#import "UILabel+BRFont.h"
#import "SVProgressHUD.h"
#import "UIView+BRCategory.h"
@interface LBBoundingIponeVC ()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView * logo;

@property (nonatomic,strong)UIButton * PasswordLogin;
@property (nonatomic,strong)UIButton * NoteLogin;
@property (nonatomic,strong)UIView * LineView;

@property (nonatomic,strong)UITextField * PhoneNumTF;/**账号*/
@property (nonatomic,strong)UITextField * PassWordTF;/**验证码*/
@property (nonatomic,strong)UITextField * codeTF;/**密码*/
@property (nonatomic,strong)UITextField * surePasswordTF;/**确认密码*/


@property (nonatomic,strong)UIView * LineView2;
@property (nonatomic,strong)UIButton * WeChatLogin;
@property (nonatomic,strong)UIView * NoteLoginView;

@property (nonatomic,strong)UIButton * ShowPassWordButton;
@property (nonatomic,strong)UIButton * CleanButton;
@property (nonatomic,strong)NSString * authId;
// 0 账号密码  1 短信    2 微信
@property (nonatomic,assign)NSInteger loginType;
@property (nonatomic,copy)NSString * openID;

@end

@implementation LBBoundingIponeVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"绑定手机";
    [self SetUI];
}
- (void)SetUI{
    
    self.logo = [[UIImageView alloc] init];
    [self.view addSubview:self.logo];
    [self.logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.centerX.mas_equalTo(self.view);
    }];
    self.logo.image = [UIImage imageNamed:@"logo"];
    
    self.PhoneNumTF = [[UITextField alloc] init];
    self.PassWordTF = [[UITextField alloc] init];
    self.codeTF = [[UITextField alloc] init];
    self.surePasswordTF = [[UITextField alloc] init];
    
    [self.view addSubview:self.PhoneNumTF];
    [self.view addSubview:self.PassWordTF];
    [self.view addSubview:self.codeTF];
    [self.view addSubview:self.surePasswordTF];
    
    
    self.PhoneNumTF.keyboardType = UIKeyboardTypeNumberPad;
    //    self.codeTF.keyboardType = UIKeyboardTypeNumberPad;
    self.PassWordTF.keyboardType = UIKeyboardTypeNumberPad;
    
    self.codeTF.secureTextEntry = YES;
    self.surePasswordTF.secureTextEntry = YES;
    self.PhoneNumTF.placeholder = @"请输入手机号";
    self.PassWordTF.placeholder = @"请输入验证码";
    self.codeTF.placeholder = @"请输入密码";
    self.surePasswordTF.placeholder = @"请再次输入密码";
    
    self.PhoneNumTF.clearButtonMode = UITextFieldViewModeAlways;
    self.PassWordTF.clearButtonMode = UITextFieldViewModeAlways;
    
    [self.PhoneNumTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.PassWordTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.codeTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.surePasswordTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    
    [self.PhoneNumTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.PassWordTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.codeTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.surePasswordTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    
    self.PassWordTF.textColor = rgb(40, 40, 40);
    self.PhoneNumTF.textColor = rgb(40, 40, 40);
    self.codeTF.textColor = rgb(40, 40, 40);
    self.surePasswordTF.textColor = rgb(40, 40, 40);
    
    [self.PhoneNumTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.top.equalTo(self.logo.mas_bottom).offset(LBSetY(70));
        make.height.mas_equalTo(45);
    }];
    
    
    UIView * tempLineView2 = [[UIView alloc] init];
    tempLineView2.backgroundColor = rgb(210, 210, 210);
    [self.view addSubview:tempLineView2];
    [tempLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.PhoneNumTF);
        make.height.equalTo(@1);
        make.right.equalTo(-15);
        
        make.top.equalTo(self.PhoneNumTF.mas_bottom).offset(0);
    }];
    
    [self.PassWordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.PhoneNumTF);
        make.top.equalTo(self.PhoneNumTF.mas_bottom).offset(10);
        make.right.mas_equalTo(-128);
    }];
    
    self.PassWordTF.placeholder = @"请输入验证码";
    
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.PassWordTF);
        make.top.equalTo(self.PassWordTF.mas_bottom).offset(10);
        make.right.mas_equalTo(-53);
    }];
    self.codeTF.placeholder = @"请输入密码";
    
    //wk_eye
    
    UIButton *eyeBtn = [[UIButton alloc] init];
    [eyeBtn setImage:LBImage(@"wk_eye") forState:UIControlStateNormal];
    [eyeBtn setImage:LBImage(@"eye") forState:UIControlStateSelected];
    [eyeBtn setTitleColor:rgb(152, 152, 152) forState:(UIControlStateNormal)];
    [self.view addSubview:eyeBtn];
    [eyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.codeTF).mas_equalTo(-3);
        make.size.mas_equalTo(CGSizeMake(40*ScaleX, 40*ScaleX));
        make.right.mas_equalTo(-10);
    }];
    
    //    eyeBtn.titleLabel.isAdaptiveFont = @"";
    //    eyeBtn.titleLabel.font = LBFont(14);
    eyeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [eyeBtn addTarget:self action:@selector(clickEyeBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * tempLineView3 = [[UIView alloc] init];
    tempLineView3.backgroundColor = rgb(210, 210, 210);
    [self.view addSubview:tempLineView3];
    [tempLineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.codeTF);
        make.height.equalTo(@1);
        make.right.equalTo(-15);
        make.top.equalTo(self.codeTF.mas_bottom).offset(0);
    }];
    
    [self.surePasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.PassWordTF);
        make.top.equalTo(self.codeTF.mas_bottom).offset(10);
        make.right.mas_equalTo(-53);
    }];
    self.surePasswordTF.placeholder = @"请再次输入密码";
    
    
    UIButton *eyeBtn2 = [[UIButton alloc] init];
    [eyeBtn2 setImage:LBImage(@"wk_eye") forState:UIControlStateNormal];
    [eyeBtn2 setImage:LBImage(@"eye") forState:UIControlStateSelected];
    [eyeBtn2 setTitleColor:rgb(152, 152, 152) forState:(UIControlStateNormal)];
    [self.view addSubview:eyeBtn2];
    [eyeBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.surePasswordTF).mas_equalTo(-3);
        make.size.mas_equalTo(CGSizeMake(40*ScaleX, 40*ScaleX));
        make.right.mas_equalTo(-10);
    }];
    
    eyeBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [eyeBtn2 addTarget:self action:@selector(clickEyeBtn2:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * tempLineView4 = [[UIView alloc] init];
    tempLineView4.backgroundColor = rgb(210, 210, 210);
    [self.view addSubview:tempLineView4];
    [tempLineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.surePasswordTF);
        make.height.equalTo(@1);
        make.right.equalTo(-15);
        make.top.equalTo(self.surePasswordTF.mas_bottom).offset(0);
    }];
    
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    
    [btn setTitleColor:rgb(152, 152, 152) forState:(UIControlStateNormal)];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.PassWordTF);
        make.width.mas_equalTo(125);
        make.right.mas_equalTo(-10);
    }];
    btn.titleLabel.isAdaptiveFont = @"";
    btn.titleLabel.font = LBFont(14);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    [btn addTarget:self action:@selector(getCode:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * lineView4 = [[UIView alloc] init];
    lineView4.backgroundColor = [UIColor colorWithHexString:@"#7d7a80"];
    [self.view addSubview:lineView4];
    
    [lineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(btn);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(1);
    }];
    
    
    self.sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.sureButton setTitle:@"确定" forState:(UIControlStateNormal)];
    [self.sureButton addTarget:self action:@selector(clickSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.sureButton.titleLabel.isAdaptiveFont = @"";
    self.sureButton.titleLabel.font = LBFont(15);
    self.sureButton.layer.cornerRadius = 22;
    self.sureButton.layer.masksToBounds = YES;
    [self.view addSubview:self.sureButton];
    [self.sureButton setBackgroundImage:LBImage(@"wk_shadow_btn") forState:(UIControlStateNormal)];
    [self.sureButton setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.top.equalTo(tempLineView4.mas_bottom).offset(LBSetY(50));
        make.height.mas_equalTo(44);
    }];
    
    UIView * tempLine3 = [[UIView alloc] init];
    tempLine3.backgroundColor = tempLineView2.backgroundColor;
    [self.view addSubview:tempLine3];
    [tempLine3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(tempLineView2);
        make.top.equalTo(self.PassWordTF.mas_bottom);
    }];
    
}
#pragma mark - 点击确定事件
- (void)clickSureBtn:(UIButton *)sender{
    
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    NSString *ipone = self.PhoneNumTF.text;
    NSString *code = self.PassWordTF.text;
    NSString *password = self.codeTF.text;
    
    if (self.PhoneNumTF.text.length==0||self.PhoneNumTF.text==nil) {
        [MBProgressHUD showSuccess:@"请输入手机号" toView:self.view];
        return;
    }
    if (self.PassWordTF.text.length==0||self.PassWordTF.text==nil) {
        [MBProgressHUD showSuccess:@"请输入验证码" toView:self.view];
        return;
    }
    if (self.codeTF.text.length==0||self.codeTF.text==nil) {
        [MBProgressHUD showSuccess:@"请输入密码" toView:self.view];
        return;
    }
    if (self.surePasswordTF.text.length==0||self.surePasswordTF.text==nil) {
        [MBProgressHUD showSuccess:@"请输入密码" toView:self.view];
        return;
    }
    if (![self.codeTF.text isEqualToString:self.surePasswordTF.text]) {
        [MBProgressHUD showSuccess:@"两次输入密码不一致" toView:self.view];
        return;
    }
    NSString *openId = self.OpenID;
    [data setObject:ipone forKey:@"tel"];
    [data setObject:code forKey:@"code"];
    [data setObject:password forKey:@"password"];
    [data setObject:openId forKey:@"openid"];
#warning TODO - 这了如果是之前注册过的手机号 （验证码注册成功后的用户），这里是绑定不成功的，需要让后台修改一下
    [WYHXNetWorkManager post:WKBoundingIphone_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        NSString *code = responseObj[@"code"];
        NSString *message = responseObj[@"msg"];
        if ([code integerValue]==200) {
            NSDictionary *dict = (NSDictionary *)responseObj;
            NSDictionary *data = dict[@"data"];
            UserModel *model = [UserModel mj_objectWithKeyValues:data];
            [UserModel SaveUserModel:model];

            //或者直接返回到根视图控制器
            [self.navigationController popToRootViewControllerAnimated:YES];
//            NSLog(@"%@",[UserModel getUserModel]);
        }else if ([code integerValue]==203){
            [MBProgressHUD showSuccess:message toView:self.view];
            return ;
        }else if ([code integerValue]==401){
            [MBProgressHUD showSuccess:message toView:self.view];
            return ;
        }
    }];
}
#pragma mark - 返回登录页面
- (void)clickAccount{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickProtocol{
    //    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)clickEyeBtn:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.codeTF.secureTextEntry = !btn.selected;
    
}
- (void)clickEyeBtn2:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.surePasswordTF.secureTextEntry = !btn.selected;
    
}

- (void)getCode:(UIButton *)btn{
    
    // 获取验证码
    if (![self.PhoneNumTF.text isMobile]) {

        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
    [self.PassWordTF becomeFirstResponder];
    NSString *iphoneNum = self.PhoneNumTF.text;
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:iphoneNum forKey:@"tel"];
    [WYHXNetWorkManager get:WKGetCode_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@%@",responseObj,error);
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            // 开启倒计时
            [self openCountdown:btn];
        }
        
    }];

    return;
    
}
// 开启倒计时效果
-(void)openCountdown:(UIButton *)btn{
    
    btn.userInteractionEnabled = NO;
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [btn setTitle:@"重新发送" forState:UIControlStateNormal];
                
                btn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [btn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                
                btn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
